package cn.iocoder.yudao.module.blog.controller.admin.casecomment;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.blog.controller.admin.casecomment.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.casecomment.BlogCaseCommentDO;
import cn.iocoder.yudao.module.blog.convert.casecomment.BlogCaseCommentConvert;
import cn.iocoder.yudao.module.blog.service.casecomment.BlogCaseCommentService;

@Tag(name = "管理后台 - 案例评论")
@RestController
@RequestMapping("/blog/case-comment")
@Validated
public class BlogCaseCommentController {

    @Resource
    private BlogCaseCommentService caseCommentService;

    @PostMapping("/create")
    @Operation(summary = "创建案例评论")
    @PreAuthorize("@ss.hasPermission('blog:case-comment:create')")
    public CommonResult<Long> createCaseComment(@Valid @RequestBody BlogCaseCommentCreateReqVO createReqVO) {
        return success(caseCommentService.createCaseComment(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新案例评论")
    @PreAuthorize("@ss.hasPermission('blog:case-comment:update')")
    public CommonResult<Boolean> updateCaseComment(@Valid @RequestBody BlogCaseCommentUpdateReqVO updateReqVO) {
        caseCommentService.updateCaseComment(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除案例评论")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:case-comment:delete')")
    public CommonResult<Boolean> deleteCaseComment(@RequestParam("id") Long id) {
        caseCommentService.deleteCaseComment(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得案例评论")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:case-comment:query')")
    public CommonResult<BlogCaseCommentRespVO> getCaseComment(@RequestParam("id") Long id) {
        BlogCaseCommentDO caseComment = caseCommentService.getCaseComment(id);
        return success(BlogCaseCommentConvert.INSTANCE.convert(caseComment));
    }

    @GetMapping("/list")
    @Operation(summary = "获得案例评论列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blog:case-comment:query')")
    public CommonResult<List<BlogCaseCommentRespVO>> getCaseCommentList(@RequestParam("ids") Collection<Long> ids) {
        List<BlogCaseCommentDO> list = caseCommentService.getCaseCommentList(ids);
        return success(BlogCaseCommentConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得案例评论分页")
    @PreAuthorize("@ss.hasPermission('blog:case-comment:query')")
    public CommonResult<PageResult<BlogCaseCommentRespVO>> getCaseCommentPage(@Valid BlogCaseCommentPageReqVO pageVO) {
        PageResult<BlogCaseCommentDO> pageResult = caseCommentService.getCaseCommentPage(pageVO);
        return success(BlogCaseCommentConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出案例评论 Excel")
    @PreAuthorize("@ss.hasPermission('blog:case-comment:export')")
    @OperateLog(type = EXPORT)
    public void exportCaseCommentExcel(@Valid BlogCaseCommentExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BlogCaseCommentDO> list = caseCommentService.getCaseCommentList(exportReqVO);
        // 导出 Excel
        List<BlogCaseCommentExcelVO> datas = BlogCaseCommentConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "案例评论.xls", "数据", BlogCaseCommentExcelVO.class, datas);
    }

}
